//
// Created by steven on 2017/3/9.
// Copyright (c) 2017 steven. All rights reserved.
//

#import "GMAddClientDataController.h"
#import "GMGuardModel.h"


@implementation GMAddClientDataController

- (void)checkGuardWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_GUARD_CHECK params:params success:^(id object) {
        self.checkResult = object[@"data"];
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"检查失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)beginGuardWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    __weak typeof(self)weakSelf = self;
    [self requestWithType:POST url:ACTION_GUARD_BEGIN params:params success:^(id object) {
        __strong  typeof(weakSelf)strongSelf = weakSelf;
        NSDictionary *data = object[@"data"];
        weakSelf.guardModel.id = [data[@"id"] integerValue];
        weakSelf.guardModel.sn = data[@"sn"];
        weakSelf.guardModel.owner = data[@"owner"];
        weakSelf.guardModel.status = data[@"status"];
        weakSelf.guardModel.useTime = data[@"useTime"][@"date"];
        weakSelf.guardModel.qrCodeUrl = data[@"qrCodeUrl"];

        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"获取守卫失败";
                break;
            case 406:
                errorString = @"无效请求";
                break;
            case 500:
                errorString = @"创建token时出错";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (void)endGuardWithParams:(NSDictionary *)params callback:(GMCompletionCallback)callback {
    [self requestWithType:POST url:ACTION_GUARD_END params:params success:^(id object) {
        callback(nil);
    } failure:^(NSInteger statusCode, NSString *message) {
        NSString *errorString = nil;
        switch (statusCode) {
            case 400:
            case 401:
            case 403:
            case 404:
                errorString = @"添加客户失败";
                break;
            case 406:
                errorString = @"添加客户失败";
                break;
            case 500:
                errorString = @"守卫已失效";
                break;
            case SERVER_NETWORK_OFF:
                errorString = @"服务器错误";
                break;
            case USER_NETWORK_OFF:
                errorString = @"请检查手机网络";
                break;
            default:
                errorString = UNKOWN_ERROR;
                break;
        }
        callback(message ? message : errorString);
    }];
}

- (GMGuardModel *)guardModel {
    if (!_guardModel) {
        _guardModel = [[GMGuardModel alloc] init];
    }
    return _guardModel;
}

@end
